package com.example.dragon.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dragon.bean.OrderBean;
import com.example.dragon.common.Common;
import com.example.dragon.dialog.MessageDialog;
import com.example.dragon.dialog.WaitDialog;
import com.example.dragon.untis.CircleImageView;
import com.example.dragon.untis.Zuobiao;
import com.example.dragon.xmf.MainActivity;
import com.example.dragon.xmf.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * Created by Dragon on 2017/6/20.
 */

public class PdDriverAdpter extends RecyclerView.Adapter {

    private List<OrderBean> mdrivesList = new ArrayList<>();
    Context mcontext;
    private MessageDialog messageDialog;
    WaitDialog mwaitDialog;
    public PdDriverAdpter(Context mcontext, List<OrderBean> mdrivesList) {
        this.mcontext = mcontext;
        this.mdrivesList=mdrivesList;
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mcontext).inflate(R.layout.drver_layout, parent, false);
        mwaitDialog = new WaitDialog(mcontext,"2");
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final OrderBean item = mdrivesList.get(position);

        final ViewHolder viewHolder = (ViewHolder) holder;
        if (item != null) {
            viewHolder.dname.setText(item.getDname());
            Glide.with(mcontext).load(item.getAvatar()).into(viewHolder.davater);


            double d = Zuobiao.getDistance(Double.parseDouble(item.getDlng()),Double.parseDouble(item.getDlat()),
                    Double.parseDouble(item.getLng()),Double.parseDouble(item.getLat()));

            Log.i("distance",d+"--"+item.getLat()+"---"+item.getLng()+"`````"+item.getDlng()+"--"+item.getDlat());
            int diss = (int) (Math.round(d/100d)/10d);
            viewHolder.ddistance.setText(diss+"KM");
            viewHolder.dratingbar.setRating(Integer.parseInt(item.getStars()));
            viewHolder.dchoseBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.i("order",MainActivity.TOKEN+"--"+item.getDriver_id()+"--"+item.getOrders_id());
                    choseDriver(item.getOrders_id(),item.getDriver_id());
                }
            });
            if (mOnItemClickLitener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = viewHolder.getLayoutPosition();
                        mOnItemClickLitener.onItemClick(viewHolder.itemView, pos);
                    }
                });

                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int pos = viewHolder.getLayoutPosition();
                        mOnItemClickLitener.onItemLongClick(viewHolder.itemView, pos);
                        return false;
                    }
                });
            }        }
    }

    private void choseDriver(String oid,String did){
        mwaitDialog.show();
        OkHttpUtils.post()
                .url(Common.MIFENG_URL+"Orders&a=selDriver")
                .addParams("token", MainActivity.TOKEN)
                .addParams("orders_id",oid)
                .addParams("driver_id",did)
                .build()//
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.i("res",e.toString());
                    }
                    @Override
                    public void onResponse(String response, int id) {
                        mwaitDialog.dismiss();
                        Log.i("res",response.toString());
                        try {
                            JSONObject jsonObject =new JSONObject(response);
                            int code = jsonObject.getInt("code");
                            if (code==200){
                                messageDialog = new MessageDialog("选择司机",jsonObject.getString("message"),"确定",mcontext);
                                notifyDataSetChanged();
                            }else {
                                messageDialog = new MessageDialog("选择司机",jsonObject.getString("message"),"确定",mcontext);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    public int getItemCount() {
        return mdrivesList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView dname,ddistance;
        CircleImageView davater;
        Button dchoseBtn;
        RatingBar dratingbar;
        public ViewHolder(View itemView) {
            super(itemView);
            dname = (TextView)itemView.findViewById(R.id.dnameText);
            davater = (CircleImageView)itemView.findViewById(R.id.davatar);
            ddistance =(TextView)itemView.findViewById(R.id.ddistanceText);
            dchoseBtn = (Button) itemView.findViewById(R.id.dchoseBtn);
            dratingbar = (RatingBar)itemView.findViewById(R.id.dratingbar);
        }
    }
}
